<?php
namespace App\Services;
use App\Models\EvaluationModel;
use App\Models\QuestionsModel;
use App\Models\AssessLogModel;
use App\Models\TagModel;
use App\Models\BabyModel;
class EvaluationService{
    /**
     * 获取测评记录
     * @param int $member_id
     * @return array $assessList
     */
    public function getAssessList($baby_id){
        $assessList = AssessLogModel::where('baby_id',$baby_id)->get()->toArray();
        if(empty($assessList)){
            return false;
        }

        foreach($assessList as $atm=>$ast){
            $assessList[$atm]['eval_list'] = EvaluationModel::where('baby_id',$ast['baby_id'])
                ->where('baby_id',$ast['baby_id'])
                ->where('add_date', '>=', $ast['assess_date'].' 00:00:00')
                ->where('add_date', '<=', $ast['assess_date'].' 23:59:59')
                ->select('question_id','option_id','score','add_date')
                ->get()
                ->toArray();
        }

        foreach($assessList as $alm=>$alt){
            if(!empty($alt['eval_list'])){
                $assessList[$alm]['score_list'] = $this->handleData($alt['eval_list']);
            }else{
                $assessList[$alm]['score_list'] = array();
            }
            unset($assessList[$alm]['eval_list']);
        }

        foreach($assessList as $atm=>$ast){
            foreach($ast['score_list'] as $asm=>$ats){
                $taginfo = TagModel::where('id',$asm)->first()->toArray();
                $assessList[$atm]['score_list'][$asm]['tag_name'] = $taginfo['tag_name'];
            }
        }

        return $assessList;
    }

    /**
     * 插入测评日志
     * @param array $data
     * @return bool $res
     */
    public function insertAssess($data){
        $res = AssessLogModel::insert($data);
        return $res;
    }

	/**
	 * 获取测评问题对应的tagid
	 * @param array $question_id
	 * @return array $tag_id_list
	 */
	public function getQuestionTagId($question_id){
		// $question_id = implode(',',$question_id);
		$tag_list = QuestionsModel::whereIn('id', $question_id)->select('tag_id')->get()->toArray();
		$tag_id_list = array();
		foreach($tag_list as $tm=>$ts){
			$tag_id_list[] = $ts['tag_id'];
		}

		$tag_id_list = array_merge(array_unique($tag_id_list));
		return $tag_id_list;
	}

	/**
	 * 添加测评结果
	 * @param array $data
	 * @return bool $res
	 */
	public function insertEvaluation($data){
		$res = EvaluationModel::insert($data);
		return $res;
	}

	/**
	 * 获取测评结果
	 * @param int $member_id
	 * @param int $baby_id
	 * @return array $list
	 */
	public function getEvaluation($member_id,$baby_id=0){
	    // 获取测评日志
        $assess_log_list = AssessLogModel::limit(2)->orderBy('assess_date','desc')->where('member_id',$member_id)
                ->where('baby_id',$baby_id)->select('assess_date')->get()->toArray();

        if(!empty($assess_log_list)){
            foreach($assess_log_list as $alm=>$alt){
                $assess_log_list[$alm]['list'] = EvaluationModel::where('member_id',$member_id)
                    ->where('baby_id',$baby_id)
                    ->where('add_date', '>=', $alt['assess_date'].' 00:00:00')
                    ->where('add_date', '<=', $alt['assess_date'].' 23:59:59')
                    ->select('question_id','option_id','score','add_date')
                    ->get()
                    ->toArray();
            }

            foreach($assess_log_list as $atm=>$ast){
                if(!empty($ast['list'])) {
                    $assess_log_list[$atm]['score_list'] = $this->handleData($ast['list']);
                    unset($assess_log_list[$atm]['list']);
                }else{
                    unset($assess_log_list[$atm]);
                }
            }
        }else{
            $assess_log_list = array();
        }

        return $assess_log_list;
	}

    /**
     * 处理测评结果数据
     * @param $list
     * @return array $score_list
     */
    private function handleData ($list){
        $score_list = array();

        foreach($list as $itm=>$lst){
            $questionInfo = QuestionsModel::where('id',$lst['question_id'])->select('tag_id')->first()->toArray();
            if(strpos($questionInfo['tag_id'],',') !== false){
                $questionInfo['tag_id'] = explode(',',$questionInfo['tag_id']);
            }else{
                $questionInfo['tag_id'] = array($questionInfo['tag_id']);
            }
            $list[$itm]['tag_id'] = $questionInfo['tag_id'];
            unset($questionInfo);
        }

        foreach($list as $key=>$val){
            foreach($val['tag_id'] as $vtm=>$vst){
                $score_list[$vst]['list'][] = $val;
            }
        }

        foreach($score_list as $stm=>$sts){
            foreach($sts['list'] as $slm=>$slt){
                $score_list[$stm][$slt['question_id']][] = $slt['score'];
            }

            unset($score_list[$stm]['list']);
        }

        foreach($score_list as $som=>$sot){
            foreach($sot as $so=>$st){
                $score_list[$som]['score_count_list'][] = array_sum($st);
                $score_list[$som]['question_id_list'][] = $so;

                unset($score_list[$som][$so]);
            }
        }

        foreach($score_list as $sol=>$sos){
            $score_list[$sol]['tag_sum_score'] = array_sum($sos['score_count_list']);
        }
        return $score_list;
    }
}